// pages/beg/beg.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // show_authorization:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.lat && options.lng){
      this.setData({
        latitude: options.lat,
        longitude: options.lng,
        city: options.searchCity,
        searchText: options.searchText
      })
    }

    this.adjustLocation();
    
    this.setData({
      img_url: app.globalData.base_url + 'images/shopList_01.jpg'
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  //获取店铺列表
  getShopList: function () {
    var that = this;
    var latitude = this.data.latitude;
    var longitude = this.data.longitude;
    var current_latitude = this.data.current_latitude;
    var current_longitude = this.data.current_longitude;
    var city = this.data.city
    api.POST({
      params: { page: 1, limit: 100, longitude: longitude, latitude: latitude, current_longitude: current_longitude, current_latitude: current_latitude, city: city },
      url: app.globalData.base_url + 'shop/shopList',
      success: function (res) {
        wx.hideLoading()
        if (util.isEmpty(res.data.code)) {

          var searchList = res.data.data.searchList;
          var currentList = res.data.data.currentList;

          if(searchList.length<=0){
            wx.showModal({
              title: '提示',
              showCancel:false,
              content:'"'+ that.data.searchText+'"附近暂无门店',
              success: function (res) {
                
              }
            })
          }

          that.setData({
            searchList: searchList,
            currentList: currentList
          })
        }
      },
      fail: function (fail) {
        wx.hideLoading()
        util.showError("网络错误");
      }
    })
  },
  //定位
  adjustLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // console.log(res)
        that.setData({
          current_latitude: res.latitude,
          current_longitude: res.longitude,
          show_authorization: false
        })
        that.getShopList();
      },
      fail: function (fail_res) {
        // console.log(fail_res)
        if (fail_res.errMsg == "getLocation:fail auth deny" || fail_res.errMsg == "getLocation:fail:auth denied") {
          // that.showModal();
          that.setData({
            show_authorization: true
          })
        } else {
          util.showError("定位失败");
        }
      }
    })
  },

  //t跳转到地图显示
  searchMap:function(e){
    var type = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    if (type =='search'){
      var shopList_active = this.data.searchList[index];
    } else if (type == 'current'){
      var shopList_active = this.data.currentList[index];
    }
    
    wx.setStorageSync('shopList_active', shopList_active)
    var lat = this.data.latitude;
    var lng = this.data.longitude;
    wx.navigateTo({
      url: '/map/pages/searchMap/searchMap?lat=' + lat + '&lng=' + lng
    })
  },
  // 导航
  navigation: function (e) {
    var latitude = e.currentTarget.dataset.lat / 1;
    var longitude = e.currentTarget.dataset.lon / 1;
    wx.openLocation({
      latitude,
      longitude,
      scale: 16
    })
  },
  //查看店内商品
  shopProduct: function (e) {
    var shopId = e.currentTarget.dataset.shopid
    wx.navigateTo({
      url: '/map/pages/shopProduct/shopProduct?shopId=' + shopId + '&index=1'
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})